﻿using Common.Utils;
using Entity.Models.dto;
using Entity.Models.Entities;
using Entity.Models.vo;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RepositorySys.Filte;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using utils;
using utils.enums;
using utils.EX;
using Utils;

namespace RepositorySys.Controllers
{
    [CustomActionFilte]
    public class ConsumableInfoController : Controller
    {
        //耗材信息接口
        IConsumableInfoBLL _consumableInfoBLL;

        //用户接口
        IUserBLL _userBLL;

        //耗材分类
        ICategoryBLL _categoryBLL;

        public ConsumableInfoController(IConsumableInfoBLL consumableInfoBLL, IUserBLL userBLL, ICategoryBLL categoryBLL)
        {
            _consumableInfoBLL = consumableInfoBLL;
            _userBLL = userBLL;
            _categoryBLL = categoryBLL;
        }

        #region 视图

        /// <summary>
        /// 耗材信息列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddConsumableInfoView()
        {
            return View();
        }

        /// <summary>
        /// 修改视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdataConsumableInfoView()
        {
            return View();
        }
        #endregion

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <returns></returns>
        public IActionResult DownloadFile()
        {
            return File(_consumableInfoBLL.DownloadFile(), "application/octet-stream", "采购记录.xlsx");
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Upload(IFormFile file)
        {
            Stream stream= file.OpenReadStream();
            string userId = HttpContext.Session.GetString("userId");
            object data =null ;
            try
            {
                if (string.IsNullOrEmpty(userId))
                {
                    throw new MyException(500, "登录已过期");
                }
                string extension= Path.GetExtension(file.FileName);
                if ( extension.Equals(".xls") || extension.Equals(".xlsx"))
                {
                    data = _consumableInfoBLL.Upload(stream, Path.GetExtension(file.FileName), userId);
                }
                else
                {
                    throw new MyException(503, "请上传Excel文件");
                }
            }
            catch (MyException e)
            {
                return getException(data, e);
            }

            return Json(new JsonResults(200, "上传成功", data));
        }

        /// <summary>
        /// 耗材信息列表、搜索
        /// </summary>
        /// <param name="consumableInfoName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getConsumableInfoList(string consumableInfoName, string page, string limit)
        {
            List<ConsumableInfoDTO> consumableInfo = null;
            int cont = 0;
            try
            {
                consumableInfo = _consumableInfoBLL.getconsumableInfoList(consumableInfoName, page, limit, out cont);
            }
            catch (MyException e)
            {
                //获取异常信息
                return getException(consumableInfo, e);
            }
            return Json(new JsonResults(0, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), cont, consumableInfo));
        }


        /// <summary>
        /// 耗材分类下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getConsumableInfoSelect()
        {
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), _categoryBLL.getCategorySelect()));
        }


        /// <summary>
        /// 添加耗材信息
        /// </summary>
        /// <param name="consumableInfo">耗材信息信息</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult AddConsumableInfo(ConsumableInfo consumableInfo)
        {
            //状态信息
            string mgs;
            object data = null;
            if (Verify(consumableInfo, out mgs))
            {
                try
                {
                    data = _consumableInfoBLL.AddConsumableInfo(consumableInfo);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            return Json(new JsonResults(200, string.IsNullOrEmpty(mgs) ? Enums.getDescribe(UserEnums.USER_STATE.SUCCESS) : mgs, data));
        }


        /// <summary>
        /// 删除耗材信息
        /// </summary>
        /// <param name="consumableInfoId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DalateConsumableInfo(string consumableInfoId)
        {
            object data = null;
            try
            {
                data = _consumableInfoBLL.DalateConsumableInfo(consumableInfoId);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="consumableInfoIds"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DalateConsumableInfoList(List<string> consumableInfoIds)
        {
            object data = null;
            try
            {
                data = _consumableInfoBLL.DalateConsumableInfoList(consumableInfoIds);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }

        /// <summary>
        /// 修改耗材信息信息
        /// </summary>
        /// <param name="consumableInfo"></param>
        /// <returns></returns>
        public IActionResult UpdataConsumableInfo(ConsumableInfo consumableInfo)
        {
            object data = null;
            string mgs = null;
            if (Verify(consumableInfo, out mgs))
            {
                try
                {
                    data = _consumableInfoBLL.UpdataConsumableInfo(consumableInfo);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            string a = string.IsNullOrEmpty(mgs) ? Enums.getDescribe(UserEnums.USER_STATE.UPDATE) : mgs;
            return Json(new JsonResults(200, a, data));
        }

        /// <summary>
        /// 根据耗材信息id搜索(修改页面数据列表)
        /// </summary>
        /// <param name="consumableInfoid">耗材信息id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getByConsumableInfo(string consumableInfoid)
        {
            //耗材信息数据集
            ConsumableInfo consumableInfo = _consumableInfoBLL.getByConsumableInfo(consumableInfoid);

            //获取分类数据集
            List<CategoryDTO> category = _categoryBLL.getCategorySelect();
            var datas = new
            {
                consumableInfo,
                category
            };
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), datas));
        }

        /// <summary>
        /// 参数验证
        /// </summary>
        /// <param name="consumableInfo">参数</param>
        /// <returns></returns>
        private bool Verify(ConsumableInfo consumableInfo, out string mgs)
        {
            if (String.IsNullOrEmpty(consumableInfo.ConsumableName))
            {
                mgs = "耗材信息名称不能为空";
                return false;
            }
            //else if (consumableInfo.LeaderId.EndsWith("0"))
            //{
            //    mgs = "主管不能为空";
            //    return false;
            //}
            //else if (String.IsNullOrEmpty(consumableInfo.ParentId))
            //{
            //    mgs = "父耗材信息不能为空";
            //    return false;
            //}
            mgs = null;
            return true;
        }

        /// <summary>
        /// 获取异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private IActionResult getException(object data, MyException e)
        {
            var tt = e.GetExceptionResult();
            return Json(new JsonResults(tt.resultStatus, tt.resultMsg, data));
        }
    }
}
